package com.fdb.efp.console.api.rest;

import com.fdb.basic.framework.core.constant.DataRuleType;
import com.fdb.basic.framework.core.constant.PubConstant;
import com.fdb.basic.framework.core.constant.ResponseData;
import com.fdb.basic.framework.web.controller.AbstractController;
import com.fdb.efp.console.service.facade.CusHandoverLstService;
import com.fdb.efp.console.service.vo.CusHandoverLstVO;
import com.fdb.efp.console.service.facade.SRoleDataRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api")
public class CusHandoverLstController extends AbstractController {

	@Autowired
	@Qualifier("cusHandoverLstService")
	private CusHandoverLstService cusHandoverLstService;
	@Autowired
	@Qualifier("sRoleDataRuleService")
	private SRoleDataRuleService sRoleDataRuleService;

	/**
	 * 列表数据查询
	 * 
	 * @param page
	 * @param size
	 * @return
	 */
	@RequestMapping(value = "/cus/handover/lsts", method = RequestMethod.POST)
	public @ResponseBody ResponseData<List<CusHandoverLstVO>> queryCusHandoverLstAll(@RequestBody CusHandoverLstVO vo) {
		// 根据用户角色获取用户权限方案
		List<CusHandoverLstVO> outputVo = null;
		Map<String,String> dataRuleTypeMap = sRoleDataRuleService.getDataRuleType(httpRequest.getRequestURI(), getUserRoles());
		vo.setPrdCodebyBaseInfo(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PRD_CODE));
		DataRuleType dataRuleType = DataRuleType.getDataRuleType(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PERMIS_TYPE));
		setUserInfoToVO(vo);

		// 根据权限方案调用不同的方案进行数据查询
		switch (dataRuleType) {
		case Owner:
			outputVo = cusHandoverLstService.queryAllOwner(vo);
			break;
		case CurrOrg:
			outputVo = cusHandoverLstService.queryAllCurrOrg(vo);
			break;
		case CurrDownOrg:
			outputVo = cusHandoverLstService.queryAllCurrDownOrg(vo);
			break;
		default:
			outputVo = cusHandoverLstService.queryAllOwner(vo);
			break;
		}
		return getResponseData(outputVo);
	}

	/**
	 * 根据主键查询详情
	 * 
	 * @return
	 */
	@RequestMapping(value = "/cus/handover/lst/{serno}", method = RequestMethod.GET)
	public @ResponseBody ResponseData<List<CusHandoverLstVO>> queryByPk(@PathVariable("serno") String serno) {

		CusHandoverLstVO inVo = new CusHandoverLstVO();

		inVo.setSerno(serno);
		List<CusHandoverLstVO> outputVo = cusHandoverLstService.queryAllBySerno(inVo);

		return getResponseData(outputVo);
	}

	/**
	 * 根据主键删除信息
	 * 
	 * @param cusHandoverLst
	 * @return
	 */
	@RequestMapping(value = "/cus/handover/lst", method = RequestMethod.DELETE)
	public @ResponseBody ResponseData<Integer> deleteByPk(@RequestBody List<CusHandoverLstVO> inCusHandoverLstVo) {
		int outputVo = 0;
		for (CusHandoverLstVO cusHandoverLstVO : inCusHandoverLstVo) {
			outputVo = cusHandoverLstService.deleteByPk(cusHandoverLstVO);
			outputVo += 1;
		}
		return getResponseData(outputVo);

	}

	/**
	 * 根据主键更新信息
	 * 
	 * @param inCusHandoverLstVo
	 * @return
	 */
	@RequestMapping(value = "/cus/handover/lst", method = RequestMethod.PUT)
	public @ResponseBody ResponseData<Integer> updateByPk(@RequestBody CusHandoverLstVO inCusHandoverLstVo) {
		int outputVo = cusHandoverLstService.updateByPk(inCusHandoverLstVo);
		return getResponseData(outputVo);

	}

	/**
	 * 新增数据
	 * 
	 * @param inCusHandoverLstVo
	 * @return
	 */
	@RequestMapping(value = "/cus/handover/lst", method = RequestMethod.POST)
	public @ResponseBody ResponseData<Integer> insertCusHandoverLst(@RequestBody CusHandoverLstVO inCusHandoverLstVo) {
		// int outputVo = cusHandoverLstService.insertCusHandoverLst(inCusHandoverLstVo);

		// return getResponseData(outputVo);
		return null;
	}

	/**
	 * 根据客户移交申请流水号 查找客户移交明细
	 * 
	 * @return
	 */
	@RequestMapping(value = "/cus/handover/lst/serno", method = RequestMethod.POST)
	public @ResponseBody ResponseData<List<CusHandoverLstVO>> queryByApplySerno(@RequestBody CusHandoverLstVO inCusHandoverLstVo) {

		List<CusHandoverLstVO> outputVo = cusHandoverLstService.queryDetailsBySerno(inCusHandoverLstVo);
		return getResponseData(outputVo);
	}
}
